/*
 _       __      _ __
| |     / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__  )
|__/|__/\__,_/_/_/____/
The electron alternative for Go
(c) Lea Anthony 2019-present
*/
import {EventEmitter} from 'eventemitter3'

const runtimeEmitter = new EventEmitter()

export function LogPrint(message) {
  console.log('[INFO]',message)
}

export function LogTrace(message) {
  console.log('[LOG]',message)
}

export function LogDebug(message) {
  console.log('[DEBUG]',message)
}

export function LogInfo(message) {
  console.log('[INFO]',message)
}

export function LogWarning(message) {
  console.log('[WARN]',message)
}

export function LogError(message) {
  console.log('[ERROR]',message)
}

export function LogFatal(message) {
  console.log('[FATAL]',message)
}

export function EventsOnMultiple(eventName, callback, maxCallbacks) {
  const count = runtimeEmitter.listenerCount(eventName)
  if (maxCallbacks && maxCallbacks < count){
    const first = runtimeEmitter.listeners(eventName)[0]
    runtimeEmitter.removeListener(eventName,first)
  }
  runtimeEmitter.on(eventName,callback)
}



export function EventsOff(eventName, ...additionalEventNames) {
  if (additionalEventNames && additionalEventNames.length){
    additionalEventNames.forEach(ev=>runtimeEmitter.removeAllListeners(ev))
  }
  runtimeEmitter.removeAllListeners(eventName)
}



export function EventsEmit(eventName) {
  let args = [eventName].slice.call(arguments);
  runtimeEmitter.emit(eventName,...args)
}

export function WindowReload() {
  window.location.reload()
}

export function WindowReloadApp() {
  window.location.reload()
}

export function WindowSetAlwaysOnTop(b) {

}

export function WindowSetSystemDefaultTheme() {

}

export function WindowSetLightTheme() {

}

export function WindowSetDarkTheme() {

}

export function WindowCenter() {

}

export function WindowSetTitle(title) {

}

export function WindowFullscreen() {

}

export function WindowUnfullscreen() {

}

export function WindowIsFullscreen() {

}

export function WindowGetSize() {

}

export function WindowSetSize(width, height) {

}

export function WindowSetMaxSize(width, height) {

}

export function WindowSetMinSize(width, height) {

}

export function WindowSetPosition(x, y) {

}

export function WindowGetPosition() {

}

export function WindowHide() {

}

export function WindowShow() {

}

export function WindowMaximise() {

}

export function WindowToggleMaximise() {

}

export function WindowUnmaximise() {

}

export function WindowIsMaximised() {

}

export function WindowMinimise() {

}

export function WindowUnminimise() {

}

export function WindowSetBackgroundColour(R, G, B, A) {

}

export function ScreenGetAll() {

}

export function WindowIsMinimised() {

}

export function WindowIsNormal() {

}

export function BrowserOpenURL(url) {
  window.open(url,"_blank")
}

export function Environment() {
  return {
    platform: 'linux'
  }
}

export function Quit() {

}

export function Hide() {

}

export function Show() {

}

/**
 * navigator.clipboard 貌似只有https和本地访问可以，其它不存在
 * @constructor
 */
export function ClipboardGetText() {
  if (window.navigator?.clipboard){
    return window.navigator.clipboard.readText()
  }
  try {
    const node = document.createElement('textarea')
    document.body.appendChild(node)
    node.select()
    node.focus()
    document.execCommand('paste')
    const content = node.value.trim()
    document.body.removeChild(node)
    return content
  }catch (e) {
    console.log('paste fail',e)
  }
  return ''
}

export function ClipboardSetText(text) {
  if (window.navigator?.clipboard){
    return window.navigator.clipboard.writeText(text)
  }
  try {
    const node = document.createElement('textarea')
    node.nodeValue = text
    node.value = text
    document.body.appendChild(node)
    node.select()
    document.execCommand('copy')
    document.body.removeChild(node)
  }catch (e) {
    console.log('copy fail', e)
    return ''
  }
  return ''
}
